function losses = computeLosses( config , classifier_bank_path)    

    dataset_n = length( config.datasets );

    losses = cell( dataset_n, length( classifier_bank_path ) );
    
    try
        timer = toc;
    catch
        tic;        
        timer = toc;
    end
        


    for i = 1:dataset_n

        dataset_folder = [config.res_pool_root config.datasets{i}.name '/'];
        
        dataset_start_time = toc;

        for j = 1:length( classifier_bank_path )

            classifier_name = classifier_bank_path(j).name;

            folder_name = classifier_name(1:end-9);

            save_folder = [dataset_folder folder_name '/'];

            losses{i,j} = computeLoss(config.datasets{i}, save_folder , config.res_type);            
            
            if( toc - timer > 20 )
                save( './temp/losses', 'losses' );
                disp(['time rest for this dataset ' num2str( (toc-dataset_start_time)/j*(length(classifier_bank_path)-j))]);
                timer = toc;
            end
        end        
        
        
    end
end

function loss = computeLoss( config, save_folder , res_type )
    n = length( config.src_list );
    loss = zeros( 1, n );
    
    if strcmp( res_type , config.src_type )
        name_format = @(A)(A);
    else
        name_format = @(str)( [ str( 1:end-length(res_type) ) res_type ] );
    end
    
    for i = 1:n
        loss(i) = getFrameLoss( [config.gt_root config.gt_list{i}] ,...
            [save_folder name_format(config.src_list{i})]);
    end        
end

function loss = getFrameLoss(name_gt, name_res)
    
    if( ...
        isempty( dir( name_gt ) ) ...
        ||isempty( dir( name_res ) ) )
    
        loss = NaN;        
    else

        gt = reviseType( imread( name_gt ) );
        res = reviseType( imread( name_res ) );

        gt = imresize( gt, [300 300] );
        res = imresize( res, [300 300] );

        diff = (gt - res).^2;

        loss = mean( diff(:) );
    end
end

function nimg = reviseType( img )
    if( size(img,3)==3 )
        img = rgb2gray( img );
    end
    
    nimg = double( img ) / 255.0;
end